What is @polkadot/util?
@polkadot/util is a utility library for JavaScript and TypeScript that provides a variety of helper functions for working with Polkadot and Substrate-based blockchain data. It includes functions for encoding/decoding, type conversion, and other common operations needed when interacting with blockchain data.
What are @polkadot/util's main functionalities?
Hexadecimal Conversion
This feature allows you to convert hexadecimal strings to Uint8Array and vice versa. It is useful for handling raw data in blockchain transactions.
const { hexToU8a, u8aToHex } = require('@polkadot/util');
const hex = '0x12345678';
const u8a = hexToU8a(hex);
console.log(u8a); // Uint8Array(4) [ 18, 52, 86, 120 ]
const backToHex = u8aToHex(u8a);
console.log(backToHex); // '0x12345678'
String Conversion
This feature allows you to convert strings to Uint8Array and vice versa. It is useful for encoding and decoding text data.
const { stringToU8a, u8aToString } = require('@polkadot/util');
const str = 'Hello, Polkadot!';
const u8a = stringToU8a(str);
console.log(u8a); // Uint8Array(15) [ 72, 101, 108, 108, 111, 44, 32, 80, 111, 108, 107, 97, 100, 111, 116 ]
const backToString = u8aToString(u8a);
console.log(backToString); // 'Hello, Polkadot!'
BN.js Integration
This feature provides integration with the BN.js library for handling large numbers. It is useful for performing arithmetic operations on large integers, which are common in blockchain applications.
const { BN } = require('@polkadot/util');
const num = new BN('12345678901234567890');
console.log(num.toString()); // '12345678901234567890'
const numHex = num.toString(16);
console.log(numHex); // 'ab54a98ceb1f0ad2'
Type Checking
This feature provides functions for checking the types of various data structures. It is useful for validating input data before processing it.
const { isHex, isU8a } = require('@polkadot/util');
console.log(isHex('0x12345678')); // true
console.log(isHex('12345678')); // false
console.log(isU8a(new Uint8Array([1, 2, 3, 4]))); // true
console.log(isU8a([1, 2, 3, 4])); // false
Other packages similar to @polkadot/util
bn.js
bn.js is a library for arbitrary-precision arithmetic. It is used for handling large integers, similar to the BN.js integration in @polkadot/util. However, bn.js focuses solely on arithmetic operations and does not provide the additional utility functions found in @polkadot/util.
buffer
The buffer package provides a way to handle binary data in Node.js. It offers similar functionality to the Uint8Array conversions in @polkadot/util, but it is more general-purpose and not specifically tailored for blockchain data.
ethers
ethers is a library for interacting with the Ethereum blockchain. It includes utility functions for encoding/decoding data, similar to @polkadot/util, but it is specifically designed for Ethereum rather than Polkadot/Substrate.
@polkadot/util
Various useful utility functions that are used across all projects in the @polkadot namespace. It provides utility functions with additional safety checks, allowing not only for consistent coding, but also reducing the general boilerplate.
Usage
Installation -
yarn add @polkadot/util
Functions can be imported directly from the package, e.g.
import { isHex } from '@polkadot/util';